package ari.com.http.request.parser;

import java.util.Map;

/**
 * @author amith.nambiar
 * 
 */

/*
 * This class is used for parsing the request parameters from the incoming HTTP
 * request. ParameterAware interface has been implemented by all the actions
 * which have a form submission pointing at them.
 */

public class ParamUtils {
	/**
	 * This function returns a String if the parameter is found in the map.
	 * 
	 * @param findParameter
	 * @param requestParameters
	 * @return
	 * @throws ParamNotFoundException
	 */
	public static String getParameter(final String findParameter,
			final Map<String, String[]> requestParameters)
			throws ParamNotFoundException {
		final String[] foundParameters = requestParameters.get(findParameter);
		if (foundParameters == null) {
			throw new ParamNotFoundException("Parameter " + findParameter
					+ " not found");
		}

		return foundParameters[0];
	}

	/**
	 * This function returns a String array if the parameter is found in the
	 * map.
	 * 
	 * @param findParameter
	 * @param requestParameters
	 * @return
	 * @throws ParamNotFoundException
	 */
	public static String[] getParameterArray(final String findParameter,
			final Map<String, String[]> requestParameters)
			throws ParamNotFoundException {
		final String[] foundParameters = requestParameters.get(findParameter);
		if (foundParameters == null) {
			throw new ParamNotFoundException("Parameter " + findParameter
					+ " not found");
		}

		return foundParameters;
	}

}
